/*****************************************************************************************
  Project   : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
  Authors   : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
  Date      : August 2025
  Output    : Table 5 and Figure 5 of appendix
*****************************************************************************************/


clear
set more off
cap mat drop _all

// Bias simulation
set seed 9230111

// data
use ${wd}/reg_dataRRv3_${ind}_${area}_All_case_0.dta , clear

local 3 "propic"
// Selection controls
if "`3'" == "propic" {
	foreach x of num 1/4{
		g dpropic_`x' = dpropic^`x'
		}
	global selectcnt dpropic_*
	global selecttest dpropic_1=dpropic_2=dpropic_3=dpropic_4=0
}

if "`3'" == "cert" {
	gen dterm1 = dcert1_c
	gen dterm2v1 = dcert2_c
	gen interact1 = dterm1 * dterm2

	foreach x of num 1/2{
		g dt1_`x' = dterm1^`x'
		g dt2_`x' = dterm2v1^`x'
		g in_`x' = 	interact1^`x'
		}
	global selectcnt dt1_* dt2_* in_*
	global selecttest dt1_1=dt1_2=dt2_1=dt2_2=in_1=in_2=0
}

// First stage reporting
* - F STAT
if "$FSTAT" == "F" local FPOS = 8
if "$FSTAT" == "F" local fs   "-Stats."
* - p Value
if "$FSTAT" == "p" local FPOS = 11
if "$FSTAT" == "p" local fs   "-values"


// Table parameters

	// Outside options
	global rents   dPE_n dPE_u  
	global rents2  dPE_0n dPE_1n dPE_u  
	
	// IVs
	global IV1 IV_n IV_u
	global IV2 IV_1n IV_0n IV_u

// absorbed interactions
global inter c.LP#controls
global inter2 c.LP#(${area}#year)


sort year $area $ind

// IV
qui ivreghdfe dwage  dER   dpropic_*  ${inter}  ${inter2}  ( $rents =   $IV1 )  [aw=weight],a(controls)  cluster($clustlevel) first  resid 

mat B = e(b)
mat i1 = B[1,"46b.controls#co.LP" .. "187.controls#c.LP"]
mat i2 = B[1,"1b.city_plus2#2b.year2#c.LP".."108o.city_plus2#4o.year2#co.LP"]



mata:
   i1 = st_matrix("i1")
   i2 = st_matrix("i2")
   gS = mean(i1')
   hS = mean(i2')
   st_numscalar("gS", gS)
   st_numscalar("hS", hS)
end

di gS
di hS

predict ehat, resid
sum ehat
local sdd = `r(sd)'


cap mat drop out B i1 i2
qui {
forval i = 1/1000 {
	
	sort year $area $ind 
	gen fake_e = rnormal(0, `sdd')
	gen new_thing = dP * fake_e * 1000
	
	ivreghdfe new_thing  dER  dpropic_*  ${inter}  ${inter2}  ( $rents =    $IV1 )  [aw=weight],a(controls )  
	
	mat B = e(b)
	mat i1 = B[1,"46b.controls#co.LP" .. "187.controls#c.LP"]
	mat i2 = B[1,"1b.city_plus2#2b.year2#c.LP".."108o.city_plus2#4o.year2#co.LP"]

	mata {
	   i1 = st_matrix("i1")
	   i2 = st_matrix("i2")
	   gS = mean(i1')
	   hS = mean(i2')
	   st_numscalar("gS", gS)
	   st_numscalar("hS", hS)
	}

	local gS  gS
	local hS  hS
	mat x = _b[dPE_n],_b[dPE_u], _se[dPE_n], _se[dPE_u], _b[dER], `gS', `hS'
	mat x = x /1000
	mat  colname x = B1 B2 ses1 ses2 ER G H
	mat  rowname x = `i'
	mat out = nullmat(out) \ x

	cap mat drop B i1 i2   
	drop fake_e new_thing 
	
	
	
}
}	

mat list out	

clear
svmat out, n(col)



lab var B1 "Non-Union Outside Option"
lab var B2 "Union Outside Option"
lab var E  "ER"
lab var G "mean P x ind. x Year slopes"
lab var H "mean P x city. x Year slopes"

histogram B1, bin(50) color(ltblue) fraction ///
   lc(grey%50) lw(vthin) ///  
  kdensity name(hist1, replace)

histogram B2, bin(50) color(ltblue%65) fraction  ///
  lc(grey%50) lw(vthin) ///  
  kdensity name(hist2, replace)

histogram E, bin(50) color(ltblue%65) fraction  ///
  lc(grey%50) lw(vthin) ///  
  kdensity name(hist3, replace)
  
histogram G, bin(50) color(ltblue%65) fraction  ///
  lc(grey%50) lw(vthin) ///  
  kdensity name(hist4, replace)  
  
histogram H, bin(50) color(ltblue%65) fraction  ///
  lc(grey%50) lw(vthin) ///  
  kdensity name(hist5, replace) 


* Figure 5 of the Appendix
graph combine hist1 hist2 hist3 hist4 hist5, ycommon xcommon imargin(tiny) iscale(0.55)
graph export $tables/Figure_5_Appendix.pdf, as(pdf) replace



tempfile temp
foreach var in H G ER B2 B1 {
	preserve

	collapse (mean) mean  = `var' (sd) sd = `var' (semean) se = `var' ///
		 (median) p50 = `var' (p5) p5 =`var' (p95) p95 = `var'
		 
	gen str Var = "`var'"
	drop se
		 
	capture append using `temp'		
	save `temp', replace
		 
	restore	 
}
		 
use `temp', clear		 

format mean sd  p5 p50 p95 %06.5f
order Var 

replace Var = "Non-Union Ouside Option" if Var == "B1"
replace Var = "Union Ouside Option" if Var == "B2"
replace Var = "Employment Rate" if Var == "ER"
replace Var = "Slopes: P x Ind. x Year" if Var == "G"
replace Var = "Slopes: P x City. x Year" if Var == "H"

lab var mean "Mean"
lab var sd   "SD"
lab var p50 "Median"
lab var p5  "5th pct."
lab var p95  "95th pct."


* Table 5 of the Appendix
texsave using "${tables}//Table_5_Appendix.tex",  replace frag autonumber nofix width(18cm)  rowsep(.1cm) varlabels  ///
headerlines("Coefficient &   &  &  &  & ")  ///
 title(Simulation Results \label{tab:simulation})  




